package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Point2D;

public class Enemy extends AnimatedSprite 
{
	
	
	
	private final float HPMax = 100;
	private float HP = HPMax;
	float spd = 1;
	boolean live = true;
	private static final long serialVersionUID = 1L;
	private Point2D posInicial;
	


	public void addHP(float hP) {
		HP += hP;
	}
	public float getHP()
	{
		return HP;
	}
	
	
	
	public Enemy(int index, Point2D pos, Point2D dim, Point2D sPos, Point2D sDim)
	{
		super(5, pos, dim, sPos, sDim, 0.2f, new Point(1, 3), true);
		
		switch(Game.rand.nextInt(4))
		{
			case 0:
				this.posInicial = new Point2D.Double(	900, 
														Game.rand.nextInt(Game.screenHeigth));
				break;
			case 1:
				this.posInicial = new Point2D.Double(	-100, 
														Game.rand.nextInt(Game.screenHeigth));
				break;
			case 2:
				this.posInicial = new Point2D.Double(	Game.rand.nextInt(Game.screenWidth),
														-100);
				break;
			case 3:
				this.posInicial = new Point2D.Double(	Game.rand.nextInt(Game.screenWidth),
														-700);
				break;
		}
		
		
		
		
	}
	
	public void update(Player player, boolean playerMorto)
	{
		if (!this.collision(player) || playerMorto == true)
			this.position = new Point2D.Double(position.getX() + spd * Math.cos(Math.toRadians(rotation)), 
				   						   position.getY() + spd * Math.sin(Math.toRadians(rotation)));
		this.animate = true;
		this.currentFrame.x = 1;
		
		if(playerMorto == true)
			this.rotation = lookAt(position, this.posInicial, this.getOrigin());
		else
			this.rotation = lookAt(position, player.position, this.getOrigin());
		
		super.update();
	}
	
	private float lookAt(Point2D p1, Point2D p2, Point2D origin)
    {
        float diffX = (float)(p1.getX() + origin.getX() - p2.getX());
        float diffY = (float)(p1.getY() + origin.getY() - p2.getY());
        float result = (float)Math.toDegrees((float)Math.atan2(diffY, diffX));
        result = (result - 180) % 360;
        if (result < 0)
            result += 360;
        return result;
    }
	
	public void draw(Graphics g)
	{
		super.draw(g);
		g.setColor(Color.BLACK);
		g.fillRect((int)this.getPosition().getX(), 
				(int)this.getPosition().getY() - 8, 
				(int)this.getRectangle().getWidth(), 
				(int)4);
		
		g.setColor(Color.RED);
		g.fillRect((int)this.getPosition().getX() + 1, 
				(int)this.getPosition().getY() - 7, 
				(int)((this.getRectangle().getWidth() - 2)/(this.HPMax/this.HP)), 
				(int)2);
	}
}
